//
// Created by test on 22-11-26.
//
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
#include<stdio.h>
#define BUF_SIZE 1024
int main(int argc,char* argv[]){
    if(argc<=2){
        printf("1error");
        return 1;
    }
    const char* ip=argv[1];
    int port=atoi(argv[2]);
    struct sockaddr_in serv_addr;
    bzero(&serv_addr,sizeof(serv_addr));
    serv_addr.sin_family=AF_INET;
    serv_addr.sin_port=htons(port);
    inet_pton(AF_INET,ip,&serv_addr.sin_addr);
    int sock=socket(PF_INET,SOCK_STREAM,0);
    assert(sock>=0);
    int ret=bind(sock,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
    assert(ret!=-1);
    ret=listen(sock,5);
    assert(ret!=-1);
    struct sockaddr_in client_addr;
    socklen_t client_addrlen=sizeof(client_addr);
    int connfd=accept(sock,(struct sockaddr*)&client_addr,&client_addrlen);
    if(connfd<0){
        printf("2error");
    }
    else{
        char buf[BUF_SIZE];
        memset(buf,'\0',sizeof(buf));
        ret=recv(connfd,buf,BUF_SIZE-1,0);
        printf("get %d bytes of normal date '%s\n'",ret,buf);
        memset(buf,'\0',sizeof(buf));
        ret=recv(connfd,buf,BUF_SIZE-1,MSG_OOB);
        printf("get %d bytes of oob date '%s\n'",ret,buf);
        ret=recv(connfd,buf,BUF_SIZE-1,0);
        printf("get %d bytes of normal date '%s\n'",ret,buf);
        close(connfd);
    }
    close(sock);
    return 0;
}